package net.carinae.dev.seduku.web.panels;

import java.util.Locale;

import org.apache.wicket.Session;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

/**
 * Simple panel that allows an user to change his/her session locale between
 * english and spanish.
 * 
 * @author Carlos Vara
 */
public class LanguageSwitcher extends Panel {
	
	public LanguageSwitcher(String id) {
		super(id);
		
		final Link<String> englishLnk;
		final Link<String> spanishLnk;
		final AttributeAppender engAttr = new AttributeAppender("class", new Model<String>("selected"), " ");
		final AttributeAppender spaAttr = new AttributeAppender("class", new Model<String>("selected"), " ");
		
		add(englishLnk = new Link<String>("english") {

			@Override
			public void onClick() {
				Session.get().setLocale(Locale.ENGLISH);
				engAttr.setEnabled(true);
				spaAttr.setEnabled(false);
			}
			
		});
		englishLnk.add(engAttr);
		
		add(spanishLnk = new Link<String>("spanish") {

			@Override
			public void onClick() {
				Session.get().setLocale(new Locale("es"));
				engAttr.setEnabled(false);
				spaAttr.setEnabled(true);
			}
		});
		spanishLnk.add(spaAttr);
		
		if ( Session.get().getLocale().getLanguage().equals("en")) {
			engAttr.setEnabled(true);
			spaAttr.setEnabled(false);
		}
		else if ( Session.get().getLocale().getLanguage().equals("es")) {
			engAttr.setEnabled(false);
			spaAttr.setEnabled(true);
		}
		else {
			Session.get().setLocale(Locale.ENGLISH );
			engAttr.setEnabled(false);
			spaAttr.setEnabled(true);
		}

	}

}
